package com.adams.app.listener;

import com.adams.app.common.Global;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.MapView;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Created by ChengBin on 13-12-29.
 * 
 */
public class MyLocationListener implements BDLocationListener {

	private TextView textView;
	private ProgressBar loadingProgress;
	private Handler handler;
	private MapView mapView;

	public MyLocationListener(Handler handler, TextView tvLocationText,
			ProgressBar loadingProgress) {
		this.textView = tvLocationText;
		this.loadingProgress = loadingProgress;
		this.handler = handler;
	}

	public MyLocationListener(Handler handler, MapView mapView) {
		this.handler = handler;
		this.mapView = mapView;
	}

	@Override
	public void onReceiveLocation(BDLocation location) {
		if (location == null)
			return;

		if (handler != null && textView == null && mapView != null
				&& location != null) {

			// 移动地图
			Message message = new Message();
			message.what = Global.START_POS;
			message.obj = location;
			Log.i("TEST",
					location.getLatitude() + "," + location.getLongitude());
			handler.sendMessage(message);

		} else if (textView != null && location != null
				&& location.getAddrStr() != null) {
			Global.isLocation = true;
			loadingProgress.setVisibility(View.GONE);
			textView.setText(location.getAddrStr());
		}
	}

	public void onReceivePoi(BDLocation poiLocation) {
		if (poiLocation == null) {
			return;
		}

		Message msg = new Message();
		msg.what = 6;
		msg.obj = poiLocation;
		handler.sendMessage(msg);
	}
}
